package com.sxj.PDMS.utils;


import java.text.ParseException;
import java.sql.Date;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VerifyUtil {

    public static boolean isValidChinesePhoneNumber(String phoneNumber) {
        if (phoneNumber.length()!=11){
            return false;
        }
        // 使用正则表达式验证手机号格式
        String regex = "^1[3456789]\\d{9}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phoneNumber);

        return matcher.matches();
    }

    public static boolean isValidChineseID(String idNumber) {
        if(idNumber.length()!=18){
            return false;
        }

        // 使用正则表达式验证中国居民身份证号码格式
        String regex = "^[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|X|x)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(idNumber);

        if (!matcher.matches()) {
            return false;
        }

        // 验证校验码
        return isValidChineseIDChecksum(idNumber);
    }
    private static boolean isValidChineseIDChecksum(String idNumber) {
        // 加权因子
        int[] weightFactors = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

        // 校验码对应值
        char[] checksumValues = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

        // 提取身份证号码的前17位
        String idNumberDigits = idNumber.substring(0, 17);

        // 计算校验码
        int checksum = 0;
        for (int i = 0; i < 17; i++) {
            char digit = idNumberDigits.charAt(i);
            int number = Character.getNumericValue(digit);
            checksum += number * weightFactors[i];
        }

        // 取余数
        int remainder = checksum % 11;

        // 验证校验码
        char expectedChecksum = checksumValues[remainder];
        char actualChecksum = idNumber.charAt(17);

        return expectedChecksum == actualChecksum;
    }

    public static boolean isValidEmail(String email) {
        if ((email != null) && (!email.isEmpty())) {
            return Pattern.matches("[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", email);
        }
        return false;
    }

    public static Date changeDate(java.util.Date date) throws ParseException {
        // 使用 java.sql.Date 的构造函数将其转换为 java.sql.Date
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());

        return sqlDate;
    }

    public static Date formatDate(LocalDateTime date) throws ParseException {


        // 将 LocalDateTime 转换为 ZonedDateTime
        ZonedDateTime zonedDateTime = date.atZone(ZoneId.systemDefault());

        // 将 ZonedDateTime 转换为 Instant

        java.util.Date from = Date.from(zonedDateTime.toInstant());
         return changeDate(from);
    }


    public static String formatDate(java.util.Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

}
